package com.kzw.dao;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang3.StringUtils;

import com.kzw.entity.History;

import my.db.Page;
import my.db.QueryHelper;

public class HistoryDao {
	// 增加
		public void save(History a) {
			String sql = "insert into history values(null,?,?,?,?,?)";
			QueryHelper.update(sql,a.getUid(),a.getUname(),a.getCost(),a.getGoods(),a.getCtime());
		}

		public void delete(Long hid) {
			String sql = "delete from history where hid=?";
			QueryHelper.update(sql, hid);
		}
		
		/**
		 * 分页查询
		 * */
		public Page<History> findPage(Page<History> page, String uname) {
			List<Object> params = new ArrayList<>();
			String sql = "select * from history";
			if(StringUtils.isNotBlank(uname)) {
				sql += " where title like ?";
				params.add("%"+uname+"%");
			}
			if(StringUtils.isNotBlank(page.getField())) {
				sql += String.format(" order by %s %s", page.getField(), page.getType());
			}
			
			List<History> data = QueryHelper.queryPage(History.class, sql, page.getPage(), page.getLimit(), params.toArray());
			page.setData(data);
			
			String sql2 = sql.replaceFirst("select .*? from", "select count(*) from");
			Long cnt = QueryHelper.stat(sql2, params.toArray());
			page.setCount(cnt);			
			
			return page;
		}
		
}
